﻿using AutoMapper;
using Modules;
using Modules.Attachment;
using Modules.Project;
using PmSoft;
using System.ComponentModel.DataAnnotations;

namespace ManagementApi.Models.Project;

/// <summary>
/// 保证金账户Dto
/// </summary>
public class ProjectGuaranteeAccountDto
{
    /// <summary>
    ///
    ///</summary>
    public int Id { get; set; }

    /// <summary>
    /// 保证金类型（1：保证金账户 2：工程担保公司保函账户 3：工程保证保险 4：银行保函）
    ///</summary>
    public 保证金类型 Type { get; set; }

    /// <summary>
    /// 保证金类型
    /// </summary>
    public string? TypeName { get; set; }

    /// <summary>
    /// 保证金账户名称/工程担保公司名称/保险公司名称/担保银行名称
    ///</summary>
    public string? Name { get; set; }

    /// <summary>
    /// 账户编号
    ///</summary>
    public string? Code { get; set; }

    /// <summary>
    /// 开户银行
    ///</summary>
    public string? BankName { get; set; }

    /// <summary>
    /// 开户银行账号
    ///</summary>
    public long? BankNo { get; set; }

    /// <summary>
    /// 人设部名称
    ///</summary>
    public string? RsName { get; set; }

    /// <summary>
    /// 工程担保公司社会信用代码/保险公司信用代码/担保银行统一信用代码
    ///</summary>
    public string? Socialcreditcode { get; set; }

    /// <summary>
    /// 工程担保公司负责人/保险公司负责人
    ///</summary>
    public string? Superintendent { get; set; }

    /// <summary>
    /// 工程担保公司负责人联系电话/保险公司负责人电话
    ///</summary>
    public string? SuperintendentMobile { get; set; }

    /// <summary>
    /// 工程担保公司注册金额
    ///</summary>
    public decimal? RegisteredCapital { get; set; }

    /// <summary>
    /// 创建人
    ///</summary>
    public string? Creator { get; set; }

    /// <summary>
    /// 创建时间
    ///</summary>
    public DateTime? CreateTime { get; set; }

    /************* 额外显示字段 ****************/

    /// <summary>
    /// 待审核数量
    /// </summary>
    public int? NeedCheckCount { get; set; }

    /// <summary>
    /// 营业执照附件
    /// </summary>
    public IEnumerable<AttachmentsForm<int>>? Attachments { get; set; }
}

/// <summary>
/// 保证金扩展
/// </summary>
public static class ProjectGuaranteeAccountExceptions
{
    /// <summary>
    /// 返回前端显示数据
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static ProjectGuaranteeAccountDto AsDto(this ProjectGuaranteeAccount entity)
    {
        ProjectGuaranteeAccountDto projectInfoDto = new();
        var dto = ServiceAccessor.GetService<IMapper>().Map(entity, projectInfoDto);
        dto.TypeName = dto.Type.GetDescription();
        var projectGuaranteePaymentService = ServiceAccessor.GetService<ProjectGuaranteePaymentService>();
        dto.NeedCheckCount = projectGuaranteePaymentService.GetProjectGuaranteePaymentCount(new ProjectGuaranteePaymentQuery() { GuaranteeAccountId = entity.Id, Status = 保证金状态.待核实 });
        var projectAttachmentService = ServiceAccessor.GetService<ProjectAttachmentService>();
        var attachments = projectAttachmentService.GetSysAttachmentByObjectIdAndType(dto.Id, 附件类型.营业执照, 项目附件租户.ProjectGuaranteeAccount);
        List<AttachmentsForm<int>> attach = new();
        foreach (var item in attachments)
        {
            AttachmentsForm<int> attachmentsForm = new()
            {
                AttachmentId = item.Id,
                AttachmentName = item.FriendlyName,
            };
            attach.Add(attachmentsForm);
        }
        dto.Attachments = attach;
        return dto;
    }
}